/////////////////////////////////////////////////////////////////////////////
/*/	
 *	Creator:		Joshua C. Rouzer
 *	Title:			
 *	Last Edited:	06/14/2013
 *	Summary:		Manages the players economic status
/*/
/////////////////////////////////////////////////////////////////////////////
#include "Economy.h"
#include "PlayerShip.h"
/////////////////////////////////////////////////////////////////////////////
// Function : Default constructor
// Notes	: zeros out the list to be sure
/////////////////////////////////////////////////////////////////////////////
	CEconomy* CEconomy::GetInstance( void )
	{
	static CEconomy s_Instance;

	return &s_Instance;
	}

CEconomy::CEconomy(){
	m_vPlayers.clear();
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Destructor
// Notes		: Cleans up any dynamically allocated memory
/////////////////////////////////////////////////////////////////////////////
CEconomy::~CEconomy(){
	m_vPlayers.clear();
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Add Player
// Parameters	: player - the player to be added
// Notes		: Will make sure of no duplicates
/////////////////////////////////////////////////////////////////////////////
void CEconomy::AddPlayer(CPlayerShip *player){
	int check = Search(player);
	if(check == -1){
		//Then add it
		m_vPlayers.push_back(player);
	}
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Remove Player
// Player		: player - the player to be removed
// Notes		: Will cycle through to find matching player
/////////////////////////////////////////////////////////////////////////////
void CEconomy::RemovePlayer(CPlayerShip *player){
	int check = Search(player);
	if(check != -1){
		m_vPlayers.erase(m_vPlayers.begin() + check);
	}
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Add Credits
// Parameters	: nAmount - the amount to be added
//					nPlayer - the player to be given the credits
// Notes		: Checks for the player then adds them appropriately
/////////////////////////////////////////////////////////////////////////////
void CEconomy::AddCredits(unsigned int nAmount){
	for (unsigned int i = 0; i < m_vPlayers.size(); i++)
	{
		m_vPlayers[i]->SetCredits(m_vPlayers[i]->GetCredits() + nAmount);
	}
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Subtract Credits
// Parameters	: nAmount - the amount to be remvoed
//					nPlayer - the player to be deducted the credits
// Notes		: Checks for the player then removes them appropriately
/////////////////////////////////////////////////////////////////////////////
void CEconomy::SubCredits(unsigned int nAmount, CPlayerShip *player){
	int check = Search(player);
	if(check != -1){
		player->SetCredits(player->GetCredits() - nAmount);
	}
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Search
// Parameters	: nPlayer - the player to be searched for
// Return		: The location it was found at, -1 for fail
// Notes		: Private helper function to search for the
//					provided player in the list of players
/////////////////////////////////////////////////////////////////////////////
int CEconomy::Search(CPlayerShip *player){
	for(unsigned int iter = 0; iter < m_vPlayers.size(); iter++){
		if(player == m_vPlayers[iter]){
			return iter;
		}
	}
	return -1;
}
/////////////////////////////////////////////////////////////////////////////
// Function		: BoughtShooter
// Parameters	: Player - the player to subtract the credits
// Notes		: Subtracts money for unit
/////////////////////////////////////////////////////////////////////////////
	void CEconomy::BoughtShooter(CPlayerShip *player)
	{
		player->SetCredits(player->GetCredits() - 30);
	}
	/////////////////////////////////////////////////////////////////////////////
// Function		: BoughtShielder
// Parameters	: Player - the player to subtract the credits
// Notes		: Subtracts money for unit
/////////////////////////////////////////////////////////////////////////////
	void CEconomy::BoughtShielder(CPlayerShip *player)
	{
		player->SetCredits(player->GetCredits() - 40);
	}
	/////////////////////////////////////////////////////////////////////////////
// Function		: BoughtRocketeer
// Parameters	: Player - the player to subtract the credits
// Notes		: Subtracts money for unit
/////////////////////////////////////////////////////////////////////////////
	void CEconomy::BoughtRocketeer(CPlayerShip *player)
	{
		player->SetCredits(player->GetCredits() - 50);
	}
	/////////////////////////////////////////////////////////////////////////////
// Function		: BoughtMech
// Parameters	: Player - the player to subtract the credits
// Notes		: Subtracts money for unit
/////////////////////////////////////////////////////////////////////////////
	void CEconomy::BoughtMech(CPlayerShip *player)
	{
		player->SetCredits(player->GetCredits() - 120);
	}
	/////////////////////////////////////////////////////////////////////////////
// Function		: BoughtHeli
// Parameters	: Player - the player to subtract the credits
// Notes		: Subtracts money for unit
/////////////////////////////////////////////////////////////////////////////
	void CEconomy::BoughtHeli(CPlayerShip *player)
	{
		player->SetCredits(player->GetCredits() - 100);
	}
	/////////////////////////////////////////////////////////////////////////////
// Function		: BoughtBarricade
// Parameters	: Player - the player to subtract the credits
// Notes		: Subtracts money for barricade
/////////////////////////////////////////////////////////////////////////////
	void CEconomy::BoughtBarricade(CPlayerShip *player)
	{
		player->SetCredits(player->GetCredits() - 70);
	}

/////////////////////////////////////////////////////////////////////////////
// Function		: BoughtBarricade
// Parameters	: Player - the player to subtract the credits
// Notes		: Subtracts money for barricade
/////////////////////////////////////////////////////////////////////////////
	int CEconomy::GetUnitCost(int UnitType)
	{
		if(UnitType == SHOOT)
			return 30;
		else if (UnitType == SHIELD)
			return 40;
		else if(UnitType == ROCKET)
			return 50;
		else if(UnitType == HELI)
			return 100;
		else if(UnitType == MECH)
			return 120;
		else if(UnitType == BARRICADE)
			return 70;
		else 
			return 0;
	}